﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Protobuf.Collections;
using Grpc.Core;
using sentry.Tool;
using sentrygrpc;

namespace sentry
{
    public class SentryprotocolService : Sentryprotocol.SentryprotocolBase
    {
        private Log _log;
        public SentryprotocolService(Log log)
        {
            _log = log;
        }

        public override Task<DefaultReply> Reload(DefaultRequest request, ServerCallContext context)
        {
            DefaultReply result = new DefaultReply
            {
                Message = "reload success!",
                Success = true
            };
            try
            {
                int count = TaskManager.Instance.Reload();
                result.Message = $"{count} task reload success!";
            }
            catch (Exception ex)
            {
                _log.WriteException(ex);
                result.Message = "reload error!";
                result.Success = false;
            }
            _log.WriteInfoAndConsole(result.Message);
            return Task.FromResult(result);
        }

        public override Task<DefaultReply> Start(NameRequest request, ServerCallContext context)
        {
            string name = request.Name;
            name = name.ToLower().Trim();
            DefaultReply result = new DefaultReply
            {
                Message = $"{name} start success!",
                Success = true
            };
            if (name == "all")
            {
                int count = TaskManager.Instance.StartAll();
                if (count > 0)
                {
                    result.Success = true;
                }
                else
                {
                    result.Success = false;
                }
                result.Message = $"{count} task start success!";
            }
            else
            {
                result.Success = TaskManager.Instance.Start(name);
                if (!result.Success)
                {
                    result.Message = $"{name} start fail!";
                }
            }
            _log.WriteInfoAndConsole(result.Message);
            return Task.FromResult(result);
        }

        public override Task<DefaultReply> Stop(NameRequest request, ServerCallContext context)
        {
            string name = request.Name;
            name = name.ToLower().Trim();
            DefaultReply result = new DefaultReply
            {
                Message = $"{name} stop success!",
                Success = true
            };
            if (name == "all")
            {
                int count = TaskManager.Instance.StopAll();
                if (count > 0)
                {
                    result.Success = true;
                }
                else
                {
                    result.Success = false;
                }
                result.Message = $"{count} task stop success!";
            }
            else
            {
                result.Success = TaskManager.Instance.Stop(name);
                if (!result.Success)
                {
                    result.Message = $"{name} stop fail!";
                }
            }
            _log.WriteInfoAndConsole(result.Message);
            return Task.FromResult(result);
        }

        public override Task<StatusReply> Status(NameRequest request, ServerCallContext context)
        {
            StatusReply result = new StatusReply
            {
                Message = "ok",
                Success = true
            };
            List<sentrygrpc.TaskStatus> list = TaskManager.Instance.GetStatus();
            result.Status.Add(list);
            return Task.FromResult(result);
        }

        public override Task<DefaultReply> Update(DefaultRequest request, ServerCallContext context)
        {
            DefaultReply result = new DefaultReply
            {
                Message = "load success!",
                Success = true
            };
            try
            {
                int count = TaskManager.Instance.Update();
                result.Message = $"{count} task load success!";
            }
            catch (Exception ex)
            {
                _log.WriteException(ex);
                result.Message = "load error!";
                result.Success = false;
            }
            _log.WriteInfoAndConsole(result.Message);
            return Task.FromResult(result);
        }

        public override Task<DefaultReply> Dir(DefaultRequest request, ServerCallContext context)
        {
            DefaultReply result = new DefaultReply
            {
                Message = App.TaskDir,
                Success = true
            };
            return Task.FromResult(result);
        }
    }
}
